#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
using std::vector;
using std::string;
using std::ifstream;
using std::istringstream;
using namespace std;
class Config{
public:
    Config(string key,string value)
    :_key(key)
    ,_value(value)
    {

    }

public:
    string _key;
    string _value;
};
class AliyunConfig{
public:
    static AliyunConfig *getInstance(){
        if(_ps==nullptr){
            _ps=new AliyunConfig();
        }
        return _ps;
    }
    void put(string key,string value){
        _configPairs.push_back(Config(key,value));
    }
    string get(const string &key){
        for (auto&ele:_configPairs){
            if(key==ele._key){
                return ele._value;
            }
        }
        static string emptyStr;
        return emptyStr;
    }
AliyunConfig(const AliyunConfig & rhs)=delete;
AliyunConfig & operator=(const AliyunConfig & rhs)=delete;

static void detory(){
    if(_ps){
        delete _ps;
        _ps=nullptr;
    }
}
    
private:
    AliyunConfig(){}
    vector<Config> _configPairs;
    static AliyunConfig *_ps;
};

 AliyunConfig * AliyunConfig::_ps=nullptr;

void test(){
    //读取配置文件，文件输入流
    ifstream ifs("aliyun.conf");
    if(!ifs){
        cout<<"no such a file"<<endl;
        return ;
    }
    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key,value;
        iss>>key>>value;
        AliyunConfig::getInstance()->put(key,value);
    }
    cout<<AliyunConfig::getInstance()->get("ip")<<endl;
    cout<<AliyunConfig::getInstance()->get("port")<<endl;
    cout<<AliyunConfig::getInstance()->get("username")<<endl;
    cout<<AliyunConfig::getInstance()->get("password")<<endl;
    cout<<AliyunConfig::getInstance()->get("name")<<endl;

}
int main(int argc, char * argv[]){
    test();
    return 0;
}

